package com.epoint.flinkdemo.state;

import com.epoint.flinkdemo.entity.SenSorReading;
import com.epoint.flinkdemo.source.WordCountFlat;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class CountStateTest {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 设置并行度为1
        env.setParallelism(1);

        DataStreamSource<String> socketStream = env.socketTextStream("localhost", 7777);

        DataStream<SenSorReading> sensorMap = socketStream.map(new MapFunction<String, SenSorReading>() {
            @Override
            public SenSorReading map(String value) throws Exception {
                String[] vars = value.split(",");
                return new SenSorReading(vars[0], Long.parseLong(vars[1]), Double.parseDouble(vars[2]));
            }
        });

        DataStream<Integer> countMap = sensorMap.map(new CountOperatorStateMapFunction());

        countMap.print();

        env.execute();
    }
}
